package com.lesso.lzh.sunflower.infra.repository.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lzh.sunflower.domain.repository.AttendanceRuleRepository;
import com.lesso.lzh.sunflower.infra.assembler.AttendanceRuleAssembler;
import com.lesso.lzh.sunflower.infra.assembler.EmployeeAssembler;
import com.lesso.lzh.sunflower.infra.mapper.AttendanceRuleMapper;
import com.lesso.lzh.sunflower.model.AttendanceRule;
import com.lesso.lzh.sunflower.model.Employee;
import com.lesso.lzh.sunflower.pojo.dto.AttendanceRuleDTO;
import com.lesso.lzh.sunflower.pojo.dto.AttendanceRuleQueryDTO;
import com.lesso.lzh.sunflower.pojo.vo.AttendanceRuleVO;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * AttendanceRuleRepositoryImpl
 *
 * @author zqz
 * @since 2025-07-04
 */
@Repository
public class AttendanceRuleRepositoryImpl implements AttendanceRuleRepository {
    private final AttendanceRuleMapper attendanceRuleMapper;

    public AttendanceRuleRepositoryImpl(AttendanceRuleMapper attendanceRuleMapper) {
        this.attendanceRuleMapper = attendanceRuleMapper;
    }

    @Override
    public Long save(AttendanceRule attendanceRule) {
        attendanceRule.setCreateTime(LocalDateTime.now());
        attendanceRule.setUpdateTime(LocalDateTime.now());
        attendanceRule.setDelStatus(0);
        attendanceRuleMapper.insert(attendanceRule);
        return attendanceRule.getId();
    }

    @Override
    public Page<AttendanceRuleVO> getPageList(AttendanceRuleQueryDTO dto) {
        Page<AttendanceRuleVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        EntityWrapper<AttendanceRule> wrapper = new EntityWrapper<>();
        wrapper.like(StringUtils.hasText(dto.getRuleName()), "rule_name", dto.getRuleName());
        List<AttendanceRule> attendanceRules = attendanceRuleMapper.selectPage(page, wrapper);
        return page.setRecords(AttendanceRuleAssembler.INSTANCE.entityListToVOList(attendanceRules));
    }

    @Override
    public Page<AttendanceRuleVO> getCustomPageList(AttendanceRuleQueryDTO dto) {
        Page<AttendanceRuleVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        List<AttendanceRuleVO> list = attendanceRuleMapper.selectPageList(page, dto);
        return page.setRecords(list);
    }

    @Override
    public void removeById(Long id) {
        attendanceRuleMapper.deleteById(id);
    }

    @Override
    public AttendanceRule selectById(Long id) {
        return attendanceRuleMapper.selectById(id);
    }

    @Override
    public void updateById(AttendanceRuleDTO dto) {
        AttendanceRule entity = AttendanceRuleAssembler.INSTANCE.dtoToEntity(dto);
        attendanceRuleMapper.updateById(entity);

    }
}